package com.link.listener;

import com.link.notify.LinkEmailNotify;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.context.event.EventListener;

import javax.annotation.Resource;

/**
 * 监听eureka相关事件，比如服务下线
 * @author 郭闪
 * @date 2019/12/23
 */

//@Component
public class EurekaStateChangeListener {

    /**
     * 注入邮件通知bean
     * @author 郭闪
     * @date 2019/12/25
     */
    @Resource
    private LinkEmailNotify emailNotify;

    /**
     * 监听eureka下线事件，然后发送邮件通知
     * @author 郭闪
     * @date 2019/12/25
     * @param event eureka 下线事件
     *
     */
    @EventListener(condition = "#event.replication==false")
    public void listen(EurekaInstanceCanceledEvent event) {
        //服务断线事件
        String serverId = event.getServerId();
        emailNotify.notify(serverId + "下线了");
    }

}
